var vm = new Vue({
    el: '#contentContainer',
    data: {
        tableData: [],
        pageNum: 1,
        pageSize: 20,
        pageSizes: [20, 30, 40, 50],
        total: 0,
        contentStatus: null,
        keyword: '',
        channelId: null,
        multipleSelection: [],
        tableHeight: 400,
        statusOptions: [],
        treeData: [{id: 0, text: "栏目"}],
        treeProps: {
            children: 'children',
            label: 'text'
        }
    },
    created: function () {
        var $this = this;

        Common.getDictListByType(dictTypeKey.contentStatusKey, function (dictList) {
            $this.statusOptions = dictList;

            Common.getJson('/cms/channel/getAllChannelList', {},
                function (response) {
                    if (!response || response.status != 200)
                        return;

                    var data = response.result;

                    // 属性配置信息
                    var attributes = {
                        // 标识字段名
                        keyField: 'channelId',
                        // 上级标识字段名
                        parentKeyField: 'parentId',
                        // 文本字段名
                        textField: 'channelName',
                        // 根节点标识
                        rootParentId: 0
                    };

                    var root = $this.treeData[0];
                    root.children = Common.convertTreeData(data, attributes);
                    $this.treeData = [root];

                    $this.loadData();
                });
        });
    },
    mounted: function () {
        //动态计算table的高度
        var winHeight = $(window).height();
        var toolbarHeight = $('#toolbar').height() + 2 || 0;
        var paginationHeight = $('#pagination').height() || 0;
        var tableMargin = parseInt($('.dataTable').css('margin-top') || 0)
            + parseInt($('.dataTable').css('margin-bottom') || 0);
        var containerPadding = parseInt($('.contentContainer').css('padding-top') || 0)
            + parseInt($('.contentContainer').css('padding-bottom') || 0);

        this.tableHeight = winHeight - toolbarHeight - paginationHeight
            - tableMargin - containerPadding;

        var treeHeight = winHeight - toolbarHeight - tableMargin - containerPadding;
        $('.tree-layout').height(treeHeight);

        //改变树形控件滚动条样式
        $('.tree').slimscroll({
            height: $(".tree-layout").height(),
            opacity: 0
        });
    },
    methods: {
        loadData: function () {
            var $this = this;

            Common.getJson('/cms/content/getContentList',
                {
                    pageNum: $this.pageNum, pageSize: $this.pageSize,
                    contentStatus: $this.contentStatus, keyword: $this.keyword,
                    channelId: $this.channelId
                },
                function (response) {
                    if (!response || response.status != 200)
                        return;

                    var result = response.result;
                    $this.tableData = result.list;
                    $this.pageNum = result.pageNum;
                    $this.pageSize = result.pageSize;
                    $this.total = result.total;
                });
        },
        onSelectionChange: function (val) {
            this.multipleSelection = val;
        },
        onSizeChange: function (size) {
            this.pageNum = 1;
            this.pageSize = size;
            this.loadData();
        },
        onCurrentChange: function (currentPage) {
            if (this.pageNum === currentPage)
                return;

            this.pageNum = currentPage;
            this.loadData();
        },
        onSearch: function () {
            this.pageNum = 1;
            this.loadData();
        },
        onAdd: function () {
            window.location.href = '/cms/content/add';
            if (this.channelId != null && this.channelId > 0) {
                window.location.href = '/cms/content/add?parentChannelId=' + this.channelId;
            }
        },
        onEdit: function () {
            if (this.multipleSelection.length == 0) {
                this.$message({
                    type: 'warning',
                    message: '请选择要编辑的数据'
                });

                return;
            }

            if (this.multipleSelection.length > 1) {
                this.$message({
                    type: 'warning',
                    message: '请只选择一条数据'
                });

                return;
            }

            var statusValid = true;
            $.each(this.multipleSelection, function (index, item) {
                if (item.contentStatus != 0 && item.contentStatus != 3) {
                    statusValid = false;
                    return;
                }
            });

            if (!statusValid) {
                this.$message({
                    type: 'error',
                    message: "只能编辑状态为【草稿】或【审核不通过】的内容"
                });

                return;
            }

            var contentId = this.multipleSelection[0].contentId;
            window.location.href = '/cms/content/edit?contentId=' + contentId;
        },
        onDelete: function () {
            if (this.multipleSelection.length == 0) {
                this.$message({
                    type: 'warning',
                    message: '请选择要删除的数据'
                });

                return;
            }

            var $this = this;
            this.$confirm('是否确认删除?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                callback: function (action, instance) {
                    if (action == 'cancel')
                        return;

                    var contentIdList = [];
                    var statusValid = true;
                    $.each($this.multipleSelection, function (index, item) {
                        if (item.contentStatus == 1) {
                            statusValid = false;
                            return;
                        }

                        contentIdList.push(item.contentId);
                    });

                    if (!statusValid) {
                        $this.$message({
                            type: 'error',
                            message: "不能删除状态为【审核中】的内容"
                        });

                        return;
                    }

                    Common.postJson('/cms/content/delete', {contentIdList: contentIdList},
                        function (response) {
                            if (!response)
                                return;

                            //删除成功
                            if (response.status == 200 && response.result.success) {
                                $this.$message({
                                    type: 'success',
                                    message: '删除成功'
                                });

                                $this.pageNum = 1;
                                $this.loadData();
                            }
                            else {
                                var errMsg = response.status == 200 ? response.result.errMsg : response.msg;

                                $this.$message({
                                    type: 'error',
                                    message: errMsg
                                });
                            }
                        });
                }
            });
        },
        onRestore: function () {
            if (this.multipleSelection.length == 0) {
                this.$message({
                    type: 'warning',
                    message: '请选择要还原的内容'
                });

                return;
            }

            var $this = this;
            this.$confirm('是否确认从回收站还原?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                callback: function (action, instance) {
                    if (action == 'cancel')
                        return;

                    var contentIdList = [];
                    var statusValid = true;
                    $.each($this.multipleSelection, function (index, item) {
                        if (item.contentStatus != 4) {
                            statusValid = false;
                            return;
                        }

                        contentIdList.push(item.contentId);
                    });

                    if (!statusValid) {
                        $this.$message({
                            type: 'error',
                            message: "只能还原状态为【回收站】的内容"
                        });

                        return;
                    }

                    Common.postJson('/cms/content/restore', {contentIdList: contentIdList},
                        function (response) {
                            if (!response)
                                return;

                            //还原成功
                            if (response.status == 200 && response.result.success) {
                                $this.$message({
                                    type: 'success',
                                    message: '还原成功'
                                });

                                $this.pageNum = 1;
                                $this.loadData();
                            }
                            else {
                                var errMsg = response.status == 200 ? response.result.errMsg : response.msg;

                                $this.$message({
                                    type: 'error',
                                    message: errMsg
                                });
                            }
                        });
                }
            });
        },
        onPublish: function () {
            if (this.multipleSelection.length == 0) {
                this.$message({
                    type: 'warning',
                    message: '请选择要发布的内容'
                });

                return;
            }

            var $this = this;
            this.$confirm('是否确认发布?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                callback: function (action, instance) {
                    if (action == 'cancel')
                        return;

                    var contentIdList = [];
                    var statusValid = true;
                    $.each($this.multipleSelection, function (index, item) {
                        if (item.contentStatus != 0) {
                            statusValid = false;
                            return;
                        }

                        contentIdList.push(item.contentId);
                    });

                    if (!statusValid) {
                        $this.$message({
                            type: 'error',
                            message: "只能发布状态为【草稿】的内容"
                        });

                        return;
                    }

                    Common.postJson('/cms/content/publish', {contentIdList: contentIdList},
                        function (response) {
                            if (!response)
                                return;

                            //发布成功
                            if (response.status == 200 && response.result.success) {
                                $this.$message({
                                    type: 'success',
                                    message: '发布成功'
                                });

                                $this.pageNum = 1;
                                $this.loadData();
                            }
                            else {
                                var errMsg = response.status == 200 ? response.result.errMsg : response.msg;

                                $this.$message({
                                    type: 'error',
                                    message: errMsg
                                });
                            }
                        });
                }
            });
        },
        preview: function (contentId) {
            window.location.href = '/cms/content/preview/' + contentId;
        },
        showAuditSteps: function (contentId) {
            window.location.href = '/process/definition/processInstanceSteps?businessKey=' + contentId;
        },
        onTreeNodeClick: function (data) {
            if (data.id == 0) {
                this.channelId = null;
            } else {
                this.channelId = data.id;
            }

            this.pageNum = 1;
            this.loadData();
        }
    }
});